/*
 * LinePanel.java
 *
 * Created on 3 juni 2008, 19:43
 */
package com.laamella.syntherface.synths.gui;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;

/**
 * Quite clumsy implementation of a panel with an arrow on it.
 * @author danny
 */
@SuppressWarnings("serial") public class LinePanel extends javax.swing.JPanel {

	private boolean horizontal = false;
	private boolean arrowA = true;
	private boolean arrowB = true;
	private int arrowHeadSize=5;

	/** Creates new form LinePanel */
	public LinePanel() {
		initComponents();
	}

	public void set(boolean horizontal, boolean arrowA, boolean arrowB){
		this.setHorizontal(horizontal);
		this.setArrowA(arrowA);
		this.setArrowB(arrowB);
	}
	
	@Override protected void paintComponent(Graphics g) {
		Graphics2D graphics = (Graphics2D) g;
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	                             RenderingHints.VALUE_ANTIALIAS_ON);
		graphics.setStroke(new BasicStroke(2));
		if (isHorizontal()) {
			drawHorizontalLine(graphics);
		} else {
			drawVerticalLine(graphics);
		}
	}

	private void drawHorizontalLine(Graphics2D graphics) {
		Point a = new Point(0, getSize().height / 2);
		Point b = new Point(getSize().width-1, getSize().height / 2);
		graphics.drawLine(a.x, a.y, b.x, b.y);
		if (isArrowA()) {
			graphics.drawLine(a.x, a.y, a.x + getArrowHeadSize(), a.y + getArrowHeadSize());
			graphics.drawLine(a.x, a.y, a.x + getArrowHeadSize(), a.y - getArrowHeadSize());
		}
		if (isArrowB()) {
			graphics.drawLine(b.x, b.y, b.x - getArrowHeadSize(), b.y + getArrowHeadSize());
			graphics.drawLine(b.x, b.y, b.x - getArrowHeadSize(), b.y - getArrowHeadSize());
		}
	}

	private void drawVerticalLine(Graphics2D graphics) {
		Point a = new Point(getSize().width / 2, 0);
		Point b = new Point(getSize().width / 2, getSize().height-1);
		graphics.drawLine(a.x, a.y, b.x, b.y);
		if (isArrowA()) {
			graphics.drawLine(a.x, a.y, a.x + getArrowHeadSize(), a.y + getArrowHeadSize());
			graphics.drawLine(a.x, a.y, a.x - getArrowHeadSize(), a.y + getArrowHeadSize());
		}
		if (isArrowB()) {
			graphics.drawLine(b.x, b.y, b.x + getArrowHeadSize(), b.y - getArrowHeadSize());
			graphics.drawLine(b.x, b.y, b.x - getArrowHeadSize(), b.y - getArrowHeadSize());
		}
	}

	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed"
	// desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setOpaque(false);
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents

	public void setHorizontal(boolean horizontal) {
		this.horizontal = horizontal;
	}

	public boolean isHorizontal() {
		return horizontal;
	}

	public void setArrowA(boolean arrowA) {
		this.arrowA = arrowA;
	}

	public boolean isArrowA() {
		return arrowA;
	}

	public void setArrowB(boolean arrowB) {
		this.arrowB = arrowB;
	}

	public boolean isArrowB() {
		return arrowB;
	}

	public void setArrowHeadSize(int arrowHeadSize) {
		this.arrowHeadSize = arrowHeadSize;
	}

	public int getArrowHeadSize() {
		return arrowHeadSize;
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
